package sw6.programExample;

import sw6.programExample.R;
import sw6.lib.Settings;
import sw6.lib.types.Interval;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	ContentObserver observer;
	
	class Observ extends ContentObserver {

		Context context;
		
		public Observ(Handler handler, Context context) {
			super(handler);
			this.context = context;
		}
		
		@Override
		public void onChange(boolean selfChange) {
			TextView output = (TextView) findViewById(R.id.systemOutput);
			output.setText("VALUE: " + Settings.getEnum(context, "myEnum"));
		}
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //InsertSettings.insertBoolean(this, "sw6.lib.appExample", "myBool", false);
        
        Interval interval = new Interval(2, 5, 0, 9);
        Settings.setStdObject(getApplicationContext(), "myInterval", interval);
        
        Button bTrue = (Button) findViewById(R.id.buttonTrue);
        bTrue.setOnClickListener(this);
        Button bFalse = (Button) findViewById(R.id.buttonFalse);
        bFalse.setOnClickListener(this);
        Button bAdd = (Button) findViewById(R.id.buttonAddOb);
        bAdd.setOnClickListener(this);
        Button bRem = (Button) findViewById(R.id.buttonRemOb);
        bRem.setOnClickListener(this);
        Button bRead = (Button) findViewById(R.id.buttonRead);
        bRead.setOnClickListener(this);
        
        TextView output = (TextView) findViewById(R.id.systemOutput);
        
        observer = new Observ(new Handler(), this);
        
        output.setText("BANG");
        //output.setText("VALUE: " + Settings.getBoolean(this, "sw6.lib.appExample", "myBool"));
        
        //Koden her bruges til generere noget data der tilhører appExample pakken. Formålet
        //er at give mulighed for at kunne køre "clear data" fra telefonen og derved teste
        //om broadcasts virker som de skal.
        
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        SharedPreferences settings = getSharedPreferences("TEST", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("silentMode", true);

        // Commit the edits!
        editor.commit();
    }

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
			case R.id.buttonTrue:
				Settings.setEnum(this, "myEnum", 1);
				break;
		
			case R.id.buttonFalse:
				Settings.setEnum(this, "myEnum", 2);
				break;
				
			case R.id.buttonAddOb:
				Settings.addEnumObserver(this, observer, "myEnum");
				break;
				
			case R.id.buttonRemOb:
				Settings.removeObserver(this, observer);
				break;
				
			case R.id.buttonRead:
				TextView output = (TextView) findViewById(R.id.systemOutput);
				output.setText("VALUE: " + Settings.getEnum(this, "myEnum"));
		}
		
	}
    
    
}